package com.wavehi.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author WaveHi
 * @Date 19/9/2021 上午 10:13
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Get the Session from database
 */
public class MyBatisUtils {
    private MyBatisUtils() {}

    //结束数据库会话工厂
    private static SqlSessionFactory ssf;

    //构建会话工厂
    static {
        //获取配置信息读取流
        InputStream in = MyBatisUtils.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //获取会话工厂构建器
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        //构建会话工厂
        ssf = ssfb.build(in);
    }

    public static SqlSession getSession() {
        return ssf.openSession();
    }
}
